﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Jy123.Web.AppCode;

namespace Jy123.Web.Module.Club
{
    public class Club_SetInfo : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string type = context.Request.QueryString["Type"];
            if (string.IsNullOrEmpty(type))
            {
                type = context.Request["Type"];
            }
            string cIdString = context.Request.QueryString["ClubId"];
            if (string.IsNullOrEmpty(cIdString))
            {
                cIdString = context.Request["ClubId"];
            }
            if (!string.IsNullOrEmpty(cIdString))
            {
                int cId = int.Parse(cIdString);
                Jy123.BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
                Jy123.Model.Club_Main club = BLLClub.GetModel(cId);
                Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
                Jy123.Model.Public_JType JType = BLLJType.GetModel(club.FJType.Value);
                //Jy123.BLL.Public_SportType BLLSportType = new Jy123.BLL.Public_SportType();
                //Jy123.Model.Public_SportType sportType = BLLSportType.GetModel(JType.FSportType.Value);
                BLL.Public_Line BLLLine = new Jy123.BLL.Public_Line();
                if (type.Equals("Load"))
                {
                    string html = context.Cache["clubsetinfo"].ToString();
                    html = html.Replace("$ClubId$", cIdString);
                    if (!string.IsNullOrEmpty(club.CName))
                    {
                        html = html.Replace("$ClubName$", club.CName);
                    }
                    else
                    {
                        html = html.Replace("$ClubName$", club.CName);
                    }
                    string strAction = string.Format("/Module/Club/Club_SetInfo.ashx?Type=Save&ClubId={0}", club.ID);
                    html = html.Replace("$Action$", strAction);
                    html = html.Replace("$ClubDescription$", club.Description);

                    if (club.FLine.HasValue)
                    {
                        Jy123.Model.Public_Line cLine = BLLLine.GetModel(club.FLine.Value);
                        html = html.Replace("$LineName$", cLine.LName);
                        html = html.Replace("$LineInfoSrc$", cLine.LModel);
                    }
                    else
                    {
                        html = html.Replace("$LineName$", string.Empty);
                        html = html.Replace("$LineInfoSrc$", "/Images/Module/QD4qy.gif");
                    }
                    if (!string.IsNullOrEmpty(club.Logo))
                    {
                        html = html.Replace("$ClubLogoSrc$", club.Logo);
                    }
                    else
                    {
                        html = html.Replace("$ClubLogoSrc$", "/Images/Module/QD4qy.gif");
                    }
                    context.Response.Write(html);
                }
                else if (type.Equals("Save"))
                {
                    //Save
                    club.CName = context.Request.Form["clubName"].ToString();
                    System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
                    if (files.Count > 0)
                    {
                        HttpPostedFile postFile = files[0];
                        string photoPath = postFile.FileName;
                        Common.Upload upload = new Jy123.Common.Upload();
                        AppCode.CreateDir createDir = new Jy123.Web.AppCode.CreateDir();
                        if (upload.IsPic(photoPath))//判断文件是否为图片类型
                        {
                            string photoType = photoPath.Substring(photoPath.LastIndexOf(".") + 1);
                            string photoName = createDir.CreatePhotoPath(CreateDir.USER, club.FUser.Value, DateTime.Now, photoType);
                            string mapPath = context.Server.MapPath("/");
                            upload.PostedFile = postFile;
                            upload.FileLength = postFile.ContentLength;
                            upload.FileName = photoName;
                            upload.Extension = photoType;
                            upload.SavePath = mapPath;
                            upload.UploadStart(photoName);
                            club.Logo = photoName;
                        }
                    }
                    string lineString = context.Request.Form["hidshirt"].ToString();
                    if (!string.IsNullOrEmpty(lineString))
                    {
                        club.FLine = int.Parse(lineString);
                    }
                    club.Description = context.Request.Form["description"].ToString();
                    BLLClub.Update(club);
                    string strRedirect = string.Format("/Module/Club/ClubInfo.aspx?Type=Load&ClubId={0}&Step=2", club.ID);
                    context.Response.Redirect(strRedirect);
                }
                else
                {
                    GetLineDiv(context, JType.FSportType.Value);
                }
            }
        }

        private void GetLineDiv(HttpContext context, int sportType)
        {
            //分页获取队服
            string html = context.Cache["clubsetinfodiv"].ToString();
            string strBlock = "display:block";
            string strNone = "display:none";
            string filter = "FSportType = " + sportType.ToString();
            int page = 1;
            string strPage = context.Request["Page"];
            if (!string.IsNullOrEmpty(strPage))
            {
                page = int.Parse(strPage);
            }
            List<int?> counts=new List<int?>();
            BLL.Line.Line BLLLine = new Jy123.BLL.Line.Line();
            List<Model.Public_Line> listLine = BLLLine.GetLineByPage(18, page, filter, out counts);
            int maxPage = int.Parse(counts[1].ToString());
            int minPage = 1;
            StringBuilder sbString = new StringBuilder();
            foreach (Jy123.Model.Public_Line line in listLine)
            {
                string strLine = context.Cache["clubsetinfoli"].ToString();
                strLine = strLine.Replace("$Id$", line.ID.ToString());
                strLine = strLine.Replace("$LineImageSrc$", line.LModel);
                strLine = strLine.Replace("$LineImageAlt$", line.LName);
                sbString.Append(strLine);
            }
            html = html.Replace("$Li$", sbString.ToString());
            string strFClass = strBlock;
            string strNClass = strBlock;
            if (page <= minPage)
            {
                strFClass = strNone;
            }
            if (page >= maxPage)
            {
                strNClass = strNone;
            }
            html = html.Replace("$frontstyle$", strFClass);
            html = html.Replace("$nextstyle$", strNClass);
            context.Response.Write(html);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
